package schedule;

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import mail.Account;
import mail.JavaMailSender;
import model.Currency;
import services.impl.CurrencyServiceImpl;
import utils.CurrencyUtils;
import utils.DateUtil;
import utils.HibernateUtil;
import view.View;

public class Scheduler {

	private int count = 1;
	private View view;
	private CurrencyUtils currencyUtils = new CurrencyUtils();
	
	private final int milisecondsPerSecond = 1000;
	private final int secondsPerMinute = 60;
	private final int minutesPerHour = 60;
	private final int hoursPerDay = 24;
	
	private final int updateHour = 9;
	private final int updateMinute = 0;
	private final int milisecondsPerDay = 
		hoursPerDay * minutesPerHour 
		* secondsPerMinute * milisecondsPerSecond;
	
	public Scheduler(View view) {
		this.view = view;
	}
	
	public void run() {
		Timer timer = new Timer();
		timer.purge();
		
		Calendar firstTime = Calendar.getInstance();
		firstTime.set(Calendar.HOUR_OF_DAY, updateHour);
		firstTime.set(Calendar.MINUTE, updateMinute);
		
		timer.schedule(new TimerTask() {
			public void run() {
				HibernateUtil.getCurrentSession().beginTransaction();
				try {
					currencyUtils.updateOnlineCurrency();
				} catch (Exception e) {
					Account account = new Account("ncms@saigontech.edu.vn", "ncms@123".toCharArray());
					String[] tos = new String[] {"halnt@saigontech.edu.vn", "ha4484@yahoo.com"};
					String[] ccs = new String[] {};
					String content = "Dear all, \n This is an automatic email - send by NCMS " +
					"- announcing that our currency updater has problem when get newest value. \n" +
					"Please update the currency manually while we fixing the error. \n " +
					"Sorry for any inconvenients. Thank you for your helps";
					JavaMailSender.mailsender.sendToMany("mail.saigontech.edu.vn", account, "NCMS currency updater has problem.", content, tos, ccs);
					//System.exit(0);
				}
				Calendar date = Calendar.getInstance();
				Currency current = CurrencyServiceImpl.INSTANCE.findCurrent();
				
				Currency currency = new Currency();
				currency.setDate(date.getTime());
				currency.setAmount(currencyUtils.getNewestValue());
				if (!DateUtil.isSame(current.getDate(), new Date())) {
					currency = CurrencyServiceImpl.INSTANCE.persist(currency);				
					HibernateUtil.getCurrentSession().getTransaction().commit();
					//update new value to view
					view.setCurrency(currency.getAmount());
					view.setCurrentDate(currency.getDate());
					view.setCount(DateUtil.toString(date.getTime(), "dd-MM") + " / " + count);
					HibernateUtil.getCurrentSession().close();
					count++;
				}
				else {
					view.setCurrency(current.getAmount());
					view.setCurrentDate(current.getDate());
					view.setCount(DateUtil.toString(current.getDate(), "dd-MM") + " / " + count);
				}
			}
		}, firstTime.getTime(), milisecondsPerDay / 4);
	}
	
	public static void main(String[] args) {
		new Scheduler(new View()).run();
	}
}